﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class frmSearchUser : Form
    {
        static string connectionString = ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        TextBox tb;
        public frmSearchUser(TextBox tb)
        {
            InitializeComponent();
            this.tb = tb;
        }
        public void Loadall()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                string sql = "select 借用人代號,姓名 from 文件借用人";
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dtgSearchUser.DataSource = dt;
                PublicClass.setRowNumber(dtgSearchUser);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void frmSearchUser_Load(object sender, EventArgs e)
        {
            Loadall();
        }

        private void btnSearchUser_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                string sqlcmd = "sp_文件借用人_尋找";
                SqlDataAdapter da = new SqlDataAdapter(sqlcmd, con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                if (txtSearchUser.Text != "") da.SelectCommand.Parameters.AddWithValue("@借用人代號或姓名", txtSearchUser.Text);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dtgSearchUser.DataSource = dt;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void btnSearchUserOK_Click(object sender, EventArgs e)
        {
            try
            {
                int r = dtgSearchUser.CurrentCell.RowIndex;
                string id = dtgSearchUser.Rows[r].Cells[0].Value.ToString();
                tb.Text = id;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtSearchUser_TextChanged(object sender, EventArgs e)
        {
            if (txtSearchUser.Text == "")
            {
                Loadall();
            }
        }
    }
}
